package com.module.ble.db

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.module.ble.db.table.UserHealthIntegratedData
import kotlinx.coroutines.flow.Flow

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2024/10/18
 * <p>
 * 描述：健康综合数据记录
 * <p>
 * 修订历史：
 */
@Dao
interface HealthIntegratedDataDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(vararg bean: UserHealthIntegratedData)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(mList:List<UserHealthIntegratedData>)

    @Query("SELECT createdTime FROM health_integrated_data WHERE createdTime IN (:times)")
    fun getExistingTimes(times: List<Long>): List<Long>

    @Query("SELECT * FROM health_integrated_data")
    fun findAll(): List<UserHealthIntegratedData>

    @Query("SELECT * FROM health_integrated_data")
    fun findAllFlow(): Flow<List<UserHealthIntegratedData>>

    // 先获取最新日期，再查询该日期的所有数据
    @Query("SELECT * FROM health_integrated_data WHERE date(createdTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(createdTime)/1000, 'unixepoch', 'localtime') FROM health_integrated_data) ORDER BY createdTime DESC")
    fun findLatestDayRecords(): List<UserHealthIntegratedData>

    @Query("SELECT * FROM health_integrated_data WHERE date(createdTime/1000, 'unixepoch', 'localtime') = (SELECT date(MAX(createdTime)/1000, 'unixepoch', 'localtime') FROM health_integrated_data) ORDER BY createdTime DESC")
    fun findLatestDayFlow(): Flow<List<UserHealthIntegratedData>>

    //根据时间范围查询数据
    @Query("SELECT * FROM health_integrated_data WHERE createdTime >= :startTime AND createdTime <= :endTime ORDER BY createdTime ASC")
    fun findByTimeRange(startTime: Long, endTime: Long): List<UserHealthIntegratedData>

    // 根据时间范围查询数据（Flow版本）
    @Query("SELECT * FROM health_integrated_data WHERE createdTime >= :startTime AND createdTime <= :endTime ORDER BY createdTime ASC")
    fun findByTimeRangeFlow(startTime: Long, endTime: Long): Flow<List<UserHealthIntegratedData>>

    // 查询 所有  倒序 查询
    @Query("SELECT * FROM health_integrated_data ORDER BY createdTime DESC  LIMIT :mSize")
    fun findByDescSize(mSize: Int): List<UserHealthIntegratedData>

    @Query("SELECT * FROM health_integrated_data WHERE createdTime = :time")
    fun findByTime(time : Long): UserHealthIntegratedData?

    @Update
    fun update(data: UserHealthIntegratedData)

    @Query("select count(*) from health_integrated_data")
    fun count(): Int

    //查询某条数据
    @Query("SELECT * FROM health_integrated_data WHERE createdTime = :time")
    fun findMessageByTime(time:Long): UserHealthIntegratedData

    @Delete
    fun delete(data: UserHealthIntegratedData)

    //删除所有 → '所有'的只能用@Query,@Query可以编写任意的sql语句
    @Query("DELETE FROM health_integrated_data")
    fun deleteAll()

    @Query("DELETE FROM health_integrated_data WHERE createdTime = :time")
    fun deleteMsgByTime(time:Long)
}